/*
 * Copyright 2011-2012 by The Regents of the University of California Licensed
 * under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License. you may obtain a copy of the
 * License from
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package edu.ucsd.db.bassarisk.storage.loading;

import java.nio.ByteBuffer;

import edu.ucsd.db.bassarisk.data.values.LongType;
import edu.ucsd.db.bassarisk.storage.common.ColumnStore;

public class VariableLengthLongColumnIndexer extends
    VariableLengthLongSegmentIndexer implements IColumnIndexer<LongType> {
  private static final short columnIndexVersion = 4;

  private final short LOCAL_HEADER_BYTES = 12;

  private final ByteBuffer drawerAddressIndexBuffer;

  public VariableLengthLongColumnIndexer() {
    super();
    this.drawerAddressIndexBuffer = ByteBuffer.allocate(ColumnStore.BYTES_IN_DRAWER);
    this.reset();
  }

  @Override
  public void indexDrawer(final ColumnSegmentInfo<LongType> drawerInfo,
                          final int drawerAddress) {
    long firstEntryIndex = drawerInfo.getFirstEntryIndex();
    long drawerEntryCount = drawerInfo.getEntryCount();

    this.bytesConsumed +=
        this.longCodec.encode(this.drawerAddressIndexBuffer, drawerAddress);

    if (this.numSegmentsIndexed == 0) {
      this.bytesConsumed +=
          this.longCodec.encode(this.entryRangeIndexBuffer, firstEntryIndex);
    }
    this.bytesConsumed +=
        this.longCodec.encode(this.entryRangeIndexBuffer, drawerEntryCount);

    this.entryCount += drawerInfo.getEntryCount();
    this.numSegmentsIndexed++;
  }

  @Override
  public void reset() {
    super.reset();
    this.drawerAddressIndexBuffer.clear();
    this.bytesConsumed += this.LOCAL_HEADER_BYTES;
  }

  @Override
  // identical to same function in "DeltaEncodedAscending column" index
  public int writeIndex(final ByteBuffer drawer, final int startCardIndex) {
    int cardsRequired = this.cardsRequired();
    int lastCardIndex = startCardIndex + cardsRequired - 1;
    assert lastCardIndex < ColumnStore.CARDS_IN_DRAWER;

    int dataPosition = this.writeCommonData(drawer, startCardIndex);
    System.arraycopy(this.drawerAddressIndexBuffer.array(), 0, drawer.array(),
                     dataPosition, this.drawerAddressIndexBuffer.position());

    // Prepare values for column index header
    int entryIndexSizeInBytes = this.entryRangeIndexBuffer.position();
    assert entryIndexSizeInBytes < 65536;

    // Prepare buffer for writing header
    int headerBytes = this.LOCAL_HEADER_BYTES + this.COMMON_HEADER_BYTES;
    int headerPositionBase = (lastCardIndex + 1) * ColumnStore.BYTES_IN_CARD;
    drawer.position(headerPositionBase - headerBytes);

    // Write header
    this.writeCommonHeaders(drawer);
    drawer.putLong(this.entryCount)
          .putShort((short) entryIndexSizeInBytes)
          .putShort(columnIndexVersion);

    // Reset buffer
    drawer.clear();

    return cardsRequired;
  }
}
